package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @projectName: sky-take-out_1
 * @package: com.sky.service.impl
 * @className: DishServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2023/10/13 19:05
 * @version: 1.0
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    @Transactional
    public void creatDish(DishDTO dishDTO) {
        Integer count = dishMapper.selectCountByName(dishDTO.getName());
        if (count!=0){
            throw new RuntimeException(MessageConstant.DUPLICATE_DISH_NAME);
        }
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setCategoryId(dishDTO.getCategoryId());
        dishMapper.creatDish(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
        }
        dishFlavorMapper.creatDishFlavor(flavors);
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Dish dishQuery = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO,dishQuery);
        List<Dish> dishes = dishMapper.selectList(dishQuery);
        PageInfo<Dish> pageInfo = new PageInfo<>(dishes);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public DishVO queryDish(Long id) {
        Dish dish = dishMapper.getDishById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.getDishFlavorById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        /*dishFlavorMapper.deleteByDishIds(Collections.singletonList(dishDTO.getId()));
        creatFlavors(dishDTO,dish);
        redisTemplate.delete(getCacheKey(dishDTO.getCategoryId()));*/
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        dishMapper.changeStatus(id,status);
    }

    @Override
    public List<Dish> queryDishByCategoryId(Long categoryId) {
        List<Dish> dish = dishMapper.queryDishByCategoryId(categoryId);
        return dish;
    }

    @Override
    @Transactional
    public void deleteBatchByIds(List<Long> ids) {
        // 1.起售的不能删除
        Integer countByIdsAndStatus = dishMapper.selectCountByIdsAndStatus(ids, StatusConstant.ENABLE);
        if (countByIdsAndStatus>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        // 2.有关联的不能删除
        Integer countByDishIds = setmealDishMapper.selectCountByDishIds(ids);
        if (countByDishIds>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 批量删除
        dishMapper.deleteBatchByIds(ids);
        // 删除口味
        dishFlavorMapper.deleteByDishIds(ids);
    }
}
